package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("shoppingCart :{}", shoppingCart);
        //1设置id 指定当前是呢个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapperDish = new LambdaQueryWrapper<>();
        queryWrapperDish.eq(ShoppingCart::getUserId, currentId);

        if (dishId != null) {
            queryWrapperDish.eq(ShoppingCart::getDishId, dishId);
        } else {
            queryWrapperDish.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //sql:select *from shopping_cart where user_id=? and dish_id/setmeal_id=?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapperDish);

        //2查询当前菜品或套餐是否在购物车中
        if (cartServiceOne != null) {
            //3如果存在 就在原来的数量下加1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //4如果不存在 则添加到购物车 数量默认位1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now()); //设置添加时间 后面排序用
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     * 这里直接用线程中保存的userId 来作为判断条件进行筛选
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车：");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);

        return R.success(shoppingCartList);
    }

    /**
     * 清除购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> cleanCart() {
        log.info("清除购物车：----");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.removeById(queryWrapper);

        return R.success("购物车清除成功");
    }

    /**
     * 减少购物车的数量
     *
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subCart(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车菜品减少：----");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        Integer cartNumber = cartServiceOne.getNumber();
        cartServiceOne.setNumber(cartNumber - 1);
        shoppingCartService.updateById(cartServiceOne);

        return R.success(cartServiceOne);
    }

}
